package com.js.study.datastructs.stack;

/**
 * @Author JiangSen
 * @DATE 2023/12/26 14:38
 */
public class MyQueue<E> implements IQueue<E>{
    E[] data;
    int size;

    public MyQueue(){
        this(5);
    }
    public MyQueue(int capacity){
        if(capacity <= 0){
            throw new RuntimeException("index not less than zero");
        }
        this.data = (E[]) new Object[capacity];
        this.size = 0;
    }
    @Override
    public void enqueue(E e) {
        if(isFull()){
            throw new RuntimeException("queue is full");
        }
        data[size] = e;
        size++;
    }

    @Override
    public E dequeue() {
        // 出队列, 并且 把元素前移
        if(isEmpty()){
            throw new RuntimeException("queue is empty");
        }
        E result = data[0];
        for(int i =1; i<size; i++){
            data[i-1] = data[i];
        }
        size--;
        data[size] = null;
        return result;
    }

    @Override
    public E getFront() {
        return data[0];
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public int getCapacity() {
        return data.length;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == data.length;
    }

    @Override
    public void print() {
        for(int i =0; i< size; i++){
            System.out.println(" "+data[i]);
        }
    }

    public static void main(String[] args) {
        MyQueue<Integer> myQueue = new MyQueue<>();

        for (int i = 0; i < 5; i++) {
            myQueue.enqueue(i + 1);
        }

        myQueue.print();
        System.out.println("");
        System.out.println("-------");
        System.out.println(myQueue.dequeue());
        System.out.println(myQueue.dequeue());
        System.out.println("-------");
        myQueue.print();
        System.out.println("");
    }

}
